<template>
  <el-container class="home-container">

    <el-aside width="90px">
      <AsideMenu @sideMenuItemClick="handleSideMenuItemClick"/>
    </el-aside>


    <div v-show="showContent" style="width: 300px;height: 100%;">
      <component v-bind:is="currentBizComp"></component>
    </div>

    <el-container>

      <el-header height="56px" class="header-tool-wrap">
        <el-button type="primary" size="medium" @click="handleAddNodeClick">新增节点</el-button>
        <el-button type="primary" size="medium" @click="handleConfigRelationClick">配置节点关系</el-button>
      </el-header>

      <el-main>
        <GraphPlay />
      </el-main>

    </el-container>

    <el-dialog
        title="新增节点"
        :visible.sync="addNodeDlgShow"
        width="40%">

      <div style="display: flex">
        <el-form ref="addNodeFormRef" :model="addNodeForm" label-width="80px">

          <el-form-item label="名称" size="small" style="width: 250px">
            <el-input v-model="addNodeForm.name" placeholder="名称"></el-input>
          </el-form-item>
          <el-form-item label="标签" size="small" style="width: 250px">
            <el-input v-model="addNodeForm.label" placeholder="标签"></el-input>
          </el-form-item>

          <el-form-item
              v-for="(item, index) in addNodeForm.props"
              :label="'属性'+(++index)">
            <el-col :span="8">
              <el-form-item size="small" style="width: 100%">
                <el-input v-model="item.name" placeholder="属性名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col style="text-align: center" :span="1">-</el-col>
            <el-col :span="8">
              <el-form-item size="small" style="width: 100%">
                <el-input v-model="item.value" placeholder="属性值"></el-input>
              </el-form-item>
            </el-col>
          </el-form-item>

        </el-form>
        <div>
          <el-tooltip placement="bottom" content="新增属性">
            <el-button class="prop-btn" size="mini" icon="el-icon-plus" @click="handleAddProp"></el-button>
          </el-tooltip>
          <el-tooltip placement="bottom" content="删除属性">
            <el-button class="prop-btn" size="mini" icon="el-icon-minus" @click="handleDeleteProp"></el-button>
          </el-tooltip>
        </div>
      </div>

      <span slot="footer" class="dialog-footer">
          <el-button @click="handleAddNodeFormCancel">取 消</el-button>
          <el-button type="primary" @click="handleAddNodeFormConfirm">确 定</el-button>
      </span>
    </el-dialog>


    <!-- 配置节点关系 -->
    <el-dialog title="配置关系"
               :visible.sync="relationConfigDlgShow"
               width="25%">
      <div style="display: flex">

        <el-form label-width="80px">
          <el-form-item label="关系名称" size="middle" >
            <el-input v-model="nodeRelationForm.relaName" placeholder="关系名称"></el-input>
          </el-form-item>

          <el-form-item label="源节点" size="middle" >
            <el-select v-model="nodeRelationForm.fromNode" placeholder="请选择源节点">
              <el-option
                  v-for="item in nodeList"
                  :key="item.id"
                  :label="item.label"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="目标节点" size="middle" >
            <el-select v-model="nodeRelationForm.toNode" placeholder="请选择目标节点">
              <el-option
                  v-for="item in nodeList"
                  :key="item.id"
                  :label="item.label"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
          <el-button @click="handleConfRelaFormCancel">取 消</el-button>
          <el-button type="primary" @click="handleConfRelaFormConfirm">确 定</el-button>
      </span>
    </el-dialog>


  </el-container>
</template>

<script>
import AsideMenu from "@/components/AsideMenu";
import DBInfo from "@/components/DBInfo";
import SysSetting from "@/components/SysSetting";

import {$NodeApi} from "@/network/node/NodeApi";
import {$RelationApi} from "@/network/node/RelationApi";

import {EventBus} from "@/EventBus";
import GraphPlay from "@/views/GraphPlay";

export default {
  name: "Home",
  components: {GraphPlay, DBInfo, AsideMenu, SysSetting},
  data() {
    return {
      showContent: true,
      showIndex: "db",
      addNodeDlgShow: false,      // 新增节点弹窗
      relationConfigDlgShow: false, // 配置关系节点弹窗
      addNodeForm: {
        id: '',
        name: '',
        label: '',
        props: [
          {name: '', value: ''}
        ]
      },
      fromNode: null,
      toNode: null,
      nodeList: [],

      nodeRelationForm: {
        relaName: '',
        fromNode: '',
        toNode: '',
      },
    }
  },
  methods: {
    handleSideMenuItemClick(param) {
      if (this.showContent && this.showIndex === param.index) {
        this.showContent = false;
      } else {
        this.showContent = true;
        this.showIndex = param.index;
      }
    },

    // 新增节点按钮点击
    handleAddNodeClick() {
      this.addNodeForm = {
        id: '',
        name: '',
        label: '',
        props: [
          {name: '', value: ''}
        ]
      }
      this.addNodeDlgShow = true;
    },

    handleAddProp() {
      this.addNodeForm.props.push({
        propName: '',
        propValue: ''
      })
    },

    handleDeleteProp() {
      if (this.addNodeForm.props.length > 1) {
        this.addNodeForm.props.pop();
      }
    },

    // 新增节点表单取消
    handleAddNodeFormCancel() {
      this.addNodeDlgShow = false;
    },

    // 新增节点表单确定
    handleAddNodeFormConfirm() {
      let params = this.addNodeForm;
      $NodeApi.saveNodeInfo(params).then(resp => {
        this.addNodeDlgShow = false;
        EventBus.$emit("nodeSave", {});
      })
    },

    handleConfigRelationClick() {
      this.relationConfigDlgShow = true;
    },

    handleConfRelaFormCancel() {
      this.relationConfigDlgShow = false;
    },

    handleConfRelaFormConfirm() {
      let params = {
        name: this.nodeRelationForm.relaName,
        fromId: this.nodeRelationForm.fromNode,
        toId: this.nodeRelationForm.toNode
      }
      if (params.fromId === params.toId) {
        this.$msg({type: 'info', message: '源节点和目标节点不能相同'});
      }
      $RelationApi.saveNodeRelation(params).then(resp => {
        let data = resp.data;
        console.log(data);
        this.$msg({type: '', message: data.content});
        if (data.code === 1) {
          this.relationConfigDlgShow = false;
          EventBus.$emit("relationSave", {});
        }
      });
    },

    /**
     * 获取节点列表
     * @returns {Promise<void>}
     */
    async getNodeList() {
      let res= await $NodeApi.listNode();
      let array = res.data;
      if (array) {
        this.nodeList = array.map(item => ({
          id: item.id,
          label: item.label
        }))
      }
    },



  },
  computed: {
    currentBizComp() {
      let compName = "";    // 要展示的组件名称
      let index = this.showIndex;
      if (index === "db") {
        return "DBInfo";
      } else if (index === "set") {
        return "SysSetting";
      }
    }
  },

  mounted() {
    this.getNodeList();
  }
}
</script>

<style scoped>

.home-container {
  height: 100%;
  display: flex;
}

.el-header {
  background-color: #E9EEF3;
  height: 56px;
}

header {
  height: 56px;
}

.el-aside {
  background-color: #525864;
}

.el-main {
  background-color: #D2D5D9;
}

.header-tool-wrap {
  display: flex;
  padding-top: 10px;
  padding-bottom: 10px;
}

.prop-btn {
  line-height: 10px;
  margin-top: 100px;
}

</style>